package com.ganxiang.ximenhong.model;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

import com.ganxiang.ximenhong.bean.AutoLoginInfo;
import com.ganxiang.ximenhong.bean.LoginInfo;
import com.ganxiang.ximenhong.config.Configs;
import com.ganxiang.ximenhong.util.Utils;

import org.json.JSONObject;

import java.io.File;
import java.util.Date;


public class CacheHandler {
	public static final String Dir_Crashlog = "crashlog", Dir_Image = "image",
			Dir_Record = "record", Dir_User = "user", Dir_API = "api", dictUseInfo = "dictUseInfo",
			Book_Import = "import";
	public static CacheHandler cacheHandler;
	
	public static CacheHandler getInstance(){
		if(cacheHandler == null){
			cacheHandler = new CacheHandler();
		}
		return cacheHandler;		
	}
	
	/**
	 * 保存友盟push消息
	 * @param c
	 * @param message
	 */
	public void saveUMPushMessage(Context c, String message){
		SharedPreferences settings = c.getSharedPreferences("UMENG_PUSH_MESSAGE", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		if (message == null) {
			editor.clear();
			editor.commit();
			return;
		}
		editor.putString("message", message);
		editor.commit();
	}
	
	/**
	 * 友盟友盟push消息
	 * @param c
	 * @return default null
	 */
	public String getUMPushMessage(Context c){
		SharedPreferences settings = c.getSharedPreferences("UMENG_PUSH_MESSAGE", Context.MODE_PRIVATE);
		String message = null;
		try {
			message = settings.getString("message", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return message;
	}
	
	/**
	 * 友盟token
	 * @param c
	 * @param token
	 */
	public void savePushToken(Context c, String token){
		SharedPreferences settings = c.getSharedPreferences("UMENG_PUSH_TOKEN", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		if (token == null) {
			editor.clear();
			editor.commit();
			return;
		}
		editor.putString("token", token);
		editor.commit();
	}
	
	/**
	 * 友盟token
	 * @param c
	 * @return default null
	 */
	public String getUMPushtoken(Context c){
		SharedPreferences settings = c.getSharedPreferences("UMENG_PUSH_TOKEN", Context.MODE_PRIVATE);
		String token = null;
		try {
			token = settings.getString("token", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return token;
	}
	






	//登录信息
	public void saveLoginInfo(Context context, LoginInfo info){
		SharedPreferences settings = context.getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		if (info == null) {
			editor.clear();
			editor.commit();
			return;
		}

		editor.putString("userToken", info.userToken);
//		editor.putString("userName", info.userName);
		editor.putString("userId", info.userId);
//		editor.putString("publickey", info.publickey);
//		editor.putString("privatekey", info.privatekey);
		editor.commit();
	}
	
	//登录信息
	public LoginInfo getLoginInfo(Context context){
		SharedPreferences settings = context.getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
		LoginInfo loginInfo = null;
		if(settings.getString("userToken", null) != null){
			loginInfo = new LoginInfo();
			loginInfo.userId = settings.getString("userId", "");
			loginInfo.userToken = settings.getString("userToken", "");
//			loginInfo.userName = settings.getString("userName", "");
//			loginInfo.publickey = settings.getString("publickey", "");
//			loginInfo.privatekey = settings.getString("privatekey", "");
		}
		
		return loginInfo;
	}
	
	// 自动登录信息
	public void saveAutoInfo(Context context, AutoLoginInfo info) {
		SharedPreferences settings = context.getSharedPreferences("AutoLoginInfo",
				Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		if (info == null) {
			editor.clear();
			editor.commit();
			return;
		}

		editor.putString("name", info.name);
		editor.putString("pwd", info.pwd);
		editor.commit();
	}

	// 自动登录信息
	public AutoLoginInfo getAutoLoginInfo(Context context) {
		SharedPreferences settings = context.getSharedPreferences("AutoLoginInfo",
				Context.MODE_PRIVATE);
		AutoLoginInfo loginInfo = null;
		if (settings.getString("name", null) != null) {
			loginInfo = new AutoLoginInfo();
			loginInfo.name = settings.getString("name", "");
			loginInfo.pwd = settings.getString("pwd", "");
		}

		return loginInfo;
	}
	
	public void saveUserInfo(Context context, JSONObject info){
		SharedPreferences settings = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		if (info == null) {
			editor.clear();
			editor.commit();
			return;
		}

		editor.putString("info", info.toString());
		editor.commit();
	}
	
	//用户信息
	public JSONObject getUserInfo(Context context){
		SharedPreferences settings = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
		JSONObject userInfo = null;
		try {
			userInfo = new JSONObject(settings.getString("info", ""));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return userInfo;
	}
	
	public File getBookDir(Context context, String bookName){
		File cacheDir = new File(CacheHandler.getInstance().getCacheDir(context), bookName == null ? "book" : "book/" + bookName);
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		
		return cacheDir;
	}
	public String getLocalBookDir(Context context){
		return getBookDir(context,"localBook").getAbsolutePath();
	}
	public File getScreenshotPath(Context context, String bookName){
		File cacheDir = new File(CacheHandler.getInstance().getCacheDir(context), bookName == null ? "book" : "screenshot/" + bookName);
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		
		return cacheDir;
	}
	public boolean isBookDir(String filePath){
		return new File(filePath).isDirectory();
	}
	
	/**
	 * 获取存储的路径， 如果sdcard不存在， 则存储在手机内存空间
	 */
	
	public File getCacheDir(Context context){
		File sdDir = null; 
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            sdDir = Environment.getExternalStorageDirectory();
        }
        else{
        	sdDir = context.getCacheDir();
        }
        
        String name = "";
		switch (Utils.getPackNmae(context)) {
		case Configs.pakeName_boruisi:
			name = Configs.pakeName_boruisi;
			break;
		
		default:
			break;
		}
        
		File cacheDir = new File(sdDir, name);
		if(!cacheDir.exists()){
			cacheDir.mkdirs();

			File noMediaFolder = new File(cacheDir, ".nomedia");
			if(!noMediaFolder.exists()){
			   noMediaFolder.mkdirs();
			}
		}
		
		return cacheDir;
	}
	
	/**
	 * 书导入的路径
	 * @param context
	 * @return
	 */
	public File getBookImportDir(Context context){
		File cacheDir = new File(CacheHandler.getInstance().getCacheDir(context), Book_Import);
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		
		return cacheDir;
	}

	/**
	 * 图片缓存目录 
	 */
	public File getImageCacheDir(Context context){
		File cacheDir = new File(getCacheDir(context), "image");
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		return cacheDir;
	}
	
	/**
	 * 用户下载图片缓存目录 
	 */
	public File getDownloadImageCacheDir(Context context){
		File cacheDir = new File(getCacheDir(context), "downloadImage");
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		return cacheDir;
	}
	
	public File getCameraDir(Context context){
		File cacheDir = new File(getCacheDir(context), "camera");
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		return cacheDir;
	}
	
	public File getCameraImgPath(Context context){
		File file = new File(getCameraDir(context), new Date().getTime() + ".jpg");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;
	}
	
	public File getCrashlogDir(Context context){
		File cacheDir = new File(getCacheDir(context), "crash");
		if(!cacheDir.exists()){
			cacheDir.mkdirs();
		}
		return cacheDir;
	}
}
